$(document).ready(
		function() {

			$('input[name=editRemoteButton]').click(function() {
				clickEditRemote($(this));
			});

			
			
			$('.remoteInput[type=submit]').click(
					function() {
						var inputFields = $(this).parent().parent().find(
								"input[type=text]");
						var ok = true;

						for (var i = 0; i < inputFields.length; i++) {
							$(inputFields[i]).css("color", "");
						}

						for (var i = 0; i < inputFields.length; i++) {
							if ($(inputFields[i]).val() === "") {
								$(inputFields[i]).css("color", "red");
								ok = false;
							}
						}

						if (!(Math.floor($(inputFields[2]).val()) == $(
								inputFields[2]).val() && $.isNumeric($(
								inputFields[2]).val()))) {
							$(inputFields[2]).css("color", "red");
							$('#errorBox').html("Ger&auml;tenummer muss eine Ganzzahl sein!").fadeIn(500).delay(5000).fadeOut(500);
							return false;
						}

						if(!ok)
							$('#errorBox').html("Fehlende Felder ausf&uuml;llen!").fadeIn(500).delay(5000).fadeOut(500);
						return ok;

					});

			$('#addRoom input[type=submit]').click(function() {
				var inputField = $(this).parent().find("input[type=text]");
				if (inputField.val() == '') {
					inputField.css("border-color", "red");
					$('#errorBox').html("Feld ausf&uuml;llen!").fadeIn(500).delay(5000).fadeOut(500);
					return false;
				}
				return true;
			});

			$('#addFloor input[type=submit]').click(function() {
				var inputField = $(this).parent().find("input[type=text]");
				if (inputField.val() == '') {
					inputField.css("border-color", "red");
					$('#errorBox').html("Feld ausf&uuml;llen!").fadeIn(500).delay(5000).fadeOut(500);
					return false;
				}
				return true;
			});
			

			$('.floorSelect').click(function() {
				var id = $(this).attr("data-floorid");
				$.ajax({
					url : "/raspi/administration.jsp",
					type : 'POST',
					data : {
						'selectedFloorId' : id
					},
					cache : false,

					// context: document.body,
					success : function(s, x) {
						var newDoc = document.open("text/html", "replace");
						newDoc.write(s);
						newDoc.close();
						// $("html").fadeOut(200).html(s).fadeIn(200);
					}
				});
			});
			

		});

function clickEditRemote(e) {
	var tds = $(e).parent().parent().find('td[name=tableData]');
	for (var i = 0; i < tds.length; i++) {
		var val = tds[i].innerHTML;
		tds[i].style.padding = "0";
		var input = document.createElement("input");
		input.setAttribute("type", "text");
		input.setAttribute("value", val);
		input.setAttribute("class", "remoteInput");
		tds[i].innerHTML = "";
		tds[i].appendChild(input);
	}
	$(e).val("Speichern");
	$(e).unbind('click');
	$(e).click(function() {
		sendEditedRemoteData($(e));
	});
	return false;
}

function sendEditedRemoteData(event) {

	var tds = [];
	var i = 0;

	event.parent().parent().find('input[type=text]').each(function() {
		tds[i++] = $(this).val();
	})

	$.post(
			"DBServlet",
			{
				"action" : "editRemote",
				"rppId" : tds[0],
				"geraetename" : tds[1],
				"hauscode" : tds[2],
				"geraetenummer" : tds[3],
				"hersteller" : tds[4]
			},
			function(result) {

				var tds = $('tr[data-fId=' + result.id + '] td');
				for (var z = 0; z < tds.length - 1; z++) {
					$(tds[z]).empty();
					tds[z].style.padding = "5px";
				}

				$(tds[tds.length - 1]).find("input")[0].value = "Ändern";
				$(tds[tds.length - 1]).find("input").first().unbind("click")
						.click(function() {
							clickEditRemote($(this));
						});

				tds[0].innerHTML = result.id;
				tds[1].innerHTML = result.geraetename;
				tds[2].innerHTML = result.hauscode;
				tds[3].innerHTML = result.geraetenummer;
				tds[4].innerHTML = result.hersteller;

				$('#infoBox').html("Erfolgreich bearbeitet.").stop(true,
						true).fadeIn(500).delay(1000).fadeOut(500);

			}, "json").done(function() {
	}).fail(function(xhr, textStatus, errorThrown) {
		$('#errorBox').html(xhr.responseText).stop(true,
				true).fadeIn(500).delay(5000).fadeOut(500, function(){
							// $('#infoBox').html("");
				});
		
	}).always(function() {

	});

}